<?php
	class submitMessagepage
	{
		public $page;
		private $succses = false;
		private $db;

		public function __construct()
		{
				$this->db = new db();
				$this->page['title'] = "send post";
				if(isset($_POST["title"]))
				{
					$this->succses = true;
					$file = $_FILES['tumbnail'];
					$extension = explode('.',$file['name']);
					$extension = strtolower($extension[count($extension)-1]);
					if($extension == "gif" || $extension == "jpg" || $extension == "jpeg" || $extension == "bmp" || $extension == "png")
					{
						$today = getdate();
						$targetPath="../uploads/".$today["mday"]."-".$today["mon"]."-".$today["year"]."/";
						
						if (!is_dir($targetPath))
						{        
							mkdir($targetPath, 0777, true);
						} 
						$targetPath = $targetPath.basename($_FILES['tumbnail']['name']);
						if(!move_uploaded_file($file['tmp_name'], $targetPath))
						{
							$this->succses = false;
							return;
						}
						if(!isset($_POST['id']))
						{
							$this->db->put("author, title, summary, content, tumbnail","bl_post",$_SESSION['user']->_id.",".$_POST["title"].",".$_POST["summary"].",".$_POST["content"].",".$targetPath);
							$this->page['content'] = "post placed succesvol";
						}
					}
					elseif(!isset($_POST['id']))
					{
						$this->succses = false;
					}
				}
				else
				{
					$this->succses = false;
				}
		}

		public function getPage()
		{
			if($this->succses)
			{
				$page = $this->page;
				require_once("templates/Main.php");
			}
			elseif(!isset($_POST['id']))
			{
				$_GET['page'] = 'newMessage';
				include('index.php');
			}
			else
			{
				$_GET['page'] = 'MessageEdit';
				$_GET['id'] = $_POST['id'];
				include('index.php');
			}
		}
	}
?>